/*
 * app_hub_api_rf_event.c
 *
 *  Created on: 2023年8月8日
 *      Author: HXF
 */


#include "app_hub_api_rf_event.h"
#include "app_hub_api_usart_event.h"
#include "string.h"
#include "app_log.h"
#include "bsp_rf.h"
#include "net_packet.h"

static unsigned char rf_tx_msg[HUB_API_RF_MSG_DATA_MAX_LEN] = {0};
static int rf_tx_len = 0;
static signed char rssi_buf[60] = {0};
static unsigned char rssi_buf_idx = 0;
static int rssi_sum = 0;
static bool rssi_buf_full = false;
static unsigned char jamming_state = 0;
static unsigned char jamming_count = 0;

#define CLEAN_BC_BUSY_TIMER_MS (491520UL) //15S

//len cmd bretry btype data
//13  0a  01     01    01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10
void DS_hub_api_creat_rf_sync_broadcast_event(const unsigned char *payload, int len)
{
  unsigned char bcast_busy = 1;
  memset(rf_tx_msg, 0, sizeof(rf_tx_msg));
  rf_tx_len = len + 2;
  rf_tx_msg[0] = len + 1;
  rf_tx_msg[1] = 0x0a;
  memcpy(&rf_tx_msg[2], payload, len);
  if(hub_log_enable){
      DS_app_log_hexdump_error("[sync buff]",rf_tx_msg, rf_tx_len);
  }
  hub_to_net_event((uint8_t *)rf_tx_msg, rf_tx_len);

  setThisParam(ENbyBcastBusy, (unsigned char *)&bcast_busy);
  start_clean_bc_busy_Timer(CLEAN_BC_BUSY_TIMER_MS, NULL);
}

//len cmd bretry btype data
//13  0b  01     01    01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10
void DS_hub_api_creat_rf_normal_broadcast_event(const unsigned char *payload, int len)
{
  memset(rf_tx_msg, 0, sizeof(rf_tx_msg));
  rf_tx_len = len + 2;
  rf_tx_msg[0] = len + 1;
  rf_tx_msg[1] = 0x0b;
  memcpy(&rf_tx_msg[2], payload, len);
  if(hub_log_enable){
      DS_app_log_hexdump_error("[normal buff]",rf_tx_msg, rf_tx_len);
  }
  hub_to_net_event((uint8_t *)rf_tx_msg, rf_tx_len);
}

//len cmd rfmid key value
//08  08  11    60  55 55 55 01 01
void DS_hub_api_creat_rf_point_to_point_event(unsigned char rfmid,
                                              const unsigned char *payload,
                                              int len)
{
  memset(rf_tx_msg, 0, sizeof(rf_tx_msg));
  rf_tx_len = len + 3;
  rf_tx_msg[0] = len + 2;
  rf_tx_msg[1] = 0x08;
  rf_tx_msg[2] = rfmid;
  memcpy(&rf_tx_msg[3], payload, len);
  if(hub_log_enable){
      DS_app_log_hexdump_error("[p2p buff]",rf_tx_msg, rf_tx_len);
  }
  hub_to_net_event((uint8_t *)rf_tx_msg, rf_tx_len);
}

void DS_hub_api_txpower_init(void)
{
  int16_t txPowerDeciDbm = 0;
  int ret = getThisParam(RfTxPower, &txPowerDeciDbm);
  if(ret != 0) {
      txPowerDeciDbm = HUB_TX_POWER_DEFAULT;
      setThisParam(RfTxPower, &txPowerDeciDbm);
  }
  RAIL_Status_t status = bsp_rf_set_tx_power_bdm(txPowerDeciDbm);
  if (status != RAIL_STATUS_NO_ERROR) {
      if(hub_log_enable){
          DS_app_log_error("set tx power error, status = 0x%x\r\n", status);
      }
  }
}

void DS_hub_api_ctune_init(void)
{
  uint32_t ctune = 0;
  int ret = getThisParam(RfCtune, &ctune);
  if(ret != 0) {
      ctune = HUB_CTUNE_DEFAULT;
      setThisParam(RfCtune, &ctune);
  }
  RAIL_Status_t status = bsp_rf_set_ctune(ctune);
  if (status != RAIL_STATUS_NO_ERROR) {
      if(hub_log_enable){
          DS_app_log_error("set ctune error, status = 0x%x\r\n", status);
      }
  }
}

void DS_hub_api_lfxo_ctune_init(void)
{
  uint8_t ctune = 0;
  int ret = getThisParam(lfxoCtune, &ctune);
  if(ret != 0) {
      ctune = HUB_LFXO_CTUNE_DEFAULT;
      setThisParam(lfxoCtune, &ctune);
  }
  CMU_LFXOInit_TypeDef lfxoInit = CMU_LFXOINIT_DEFAULT;

  lfxoInit.mode = cmuLfxoOscMode_Crystal;
  lfxoInit.capTune = ctune;
  lfxoInit.timeout = cmuLfxoStartupDelay_4KCycles;

  CMU_LFXOInit(&lfxoInit);
  CMU_LFXOPrecisionSet(100);
}

void DS_hub_api_antenna_init(void)
{
  bsp_rf_antenna_select(0);
}

void DS_hub_avg_rssi(signed char rssi)
{
  signed char real_rssi = rssi;
  signed char avg_rssi = 0;
  signed char jamming_able = -80;

  rssi_sum -= rssi_buf[rssi_buf_idx];
  if(rssi_buf_full){
      if(rssi_buf[rssi_buf_idx] > jamming_able){
          if(jamming_count){
              jamming_count--;
          }
      }
  }
  rssi_buf[rssi_buf_idx] = rssi;
  rssi_sum += rssi_buf[rssi_buf_idx];
  if(rssi_buf[rssi_buf_idx] > jamming_able){
      jamming_count++;
  }
  rssi_buf_idx++;
  if(rssi_buf_idx >= 60){
      rssi_buf_full = true;
      rssi_buf_idx = 0;
  }
  avg_rssi = (rssi_buf_full)?rssi_sum/60 : rssi_sum/rssi_buf_idx;

  setThisParam(ENbyRealRssi, &real_rssi);
  setThisParam(ENbyAvgRssi, &avg_rssi);

  if(jamming_state == 0){
      if(jamming_count > 30){
          jamming_state = 1;
          DS_hub_api_creat_hub_event(JAMMING_EVT, &jamming_state, 1);
      }
  }else{
      if(jamming_count < 10){
          jamming_state = 0;
          DS_hub_api_creat_hub_event(JAMMING_EVT, &jamming_state, 1);
      }
  }
}
